package gacl.javaweb11.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zcx
 *
 * 2017年4月17日 下午3:18:00
 * 
 * cookie实例：获取用户上一次访问的时间
 */
@WebServlet("/cookie01")
public class Cookie01 extends HttpServlet{
	
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置服务器端以UTF-8编码进行输出
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter pw = response.getWriter();
		//获取浏览器访问服务器时传递过来的cookie数组
		Cookie[] cookies = request.getCookies();
		//如果用户是第一次访问，那么得到的cookies将是null
		if(null != cookies) {
			pw.write("您上次访问的时间是：");
			for(int i=0; i<cookies.length; i++){
				Cookie cookie = cookies[i];
				if(cookie.getName().equals("lastAccessTime")){
					Long lastAccessTime = Long.parseLong(cookie.getValue());
					Date date = new Date(lastAccessTime);
					pw.write(date.toLocaleString());
				}
			}
		}else{
			pw.write("这是您第一次访问本站！");
		}
		
		//用户访问过之后重新设置用户的访问时间，存储到cookie中，然后发送到客户端浏览器
		Cookie cookie = new Cookie("lastAccessTime",System.currentTimeMillis()+"");//创建一个cookie，cookie 的名字是lastAccessTime
		//设置cookie的有效期为1天
		cookie.setMaxAge(24*60*60*1000);
		//将cookie对象添加到response对象中，这样服务器在输出response对象中的内容时就会把cookie也输出到客户端浏览器
		response.addCookie(cookie);
		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
}
